package com.qf.sy2103.ssm.controller;

import com.qf.sy2103.ssm.constants.Constants;
import com.qf.sy2103.ssm.entity.Gongneng;
import com.qf.sy2103.ssm.entity.Kehu;
import com.qf.sy2103.ssm.service.GongnengService;
import com.qf.sy2103.ssm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/gongneng")
public class GongnengController {
    @Autowired
    private GongnengService gongnengService;
    @RequestMapping("/all")
    @ResponseBody
    public TableVo<Gongneng> kehuAll(PageVo pageVo, String name, Integer state){
        Gongneng gongneng=new Gongneng();
        gongneng.setAppname(name);
        gongneng.setState(state);
        TableVo<Gongneng> all = gongnengService.all(pageVo, gongneng);
        return all;
    }
    @RequestMapping("/add")
    @ResponseBody
    public MessageVo kehuAdd(GongnengVo gongnengVo){
        System.out.println(gongnengVo);
        Gongneng gongneng=new Gongneng(gongnengVo.getCorpname(),gongnengVo.getAppname(),gongnengVo.getAppkey(),gongnengVo.getAppsecret(),gongnengVo.getRedirecturl(),gongnengVo.getInit(),gongnengVo.getDescripition(),gongnengVo.getCusid(),gongnengVo.getState());
        int add = gongnengService.add(gongneng);
        if (add==1){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDSUCCESS, Constants.USER_MSG_ADDSUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDFAIL, Constants.USER_MSG_ADDFAIL, null);
        return messageVo;
    }
    @RequestMapping("/update")
    @ResponseBody
    public MessageVo kehuEdit(GongnengVo gongnengVo){
        Gongneng gongneng=new Gongneng(gongnengVo.getCorpname(),gongnengVo.getAppname(),gongnengVo.getAppkey(),gongnengVo.getAppsecret(),gongnengVo.getRedirecturl(),gongnengVo.getInit(),gongnengVo.getDescripition(),gongnengVo.getCusid(),gongnengVo.getState());
        gongneng.setId(gongnengVo.getId());
        int edit = gongnengService.updata(gongneng);

        if (edit==1){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITSUCCESS, Constants.USER_MSG_EDITSUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITFAIL, Constants.USER_MSG_EDITFAIL, null);
        return messageVo;
    }
    @RequestMapping("/del")
    @ResponseBody
    public MessageVo kehuDel(Integer[] ids){
        int del = gongnengService.del(ids);
        if (del>0){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETESUCCESS, Constants.USER_MSG_DELETESUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETEFAIL, Constants.USER_MSG_DELETEFAIL, null);
        return messageVo;
    }
}
